<template>
  <div class="view-fill">
    <div v-if="!modifyInfo.visible" class="app-container-flex">
      <NewFullScreen
        v-model="showDialog"
        title="省采订单支付详情"
        @returnBack="returnBack"
      >
        <template v-slot:full-button>
          <el-button
            v-if="
              $permission('provincePayOrder:update') &&
              infoList.data.status === 0
            "
            @click="update"
            >修改</el-button
          >
          <el-button v-if="canSubmit" @click="submit">提交 </el-button>
          <el-button v-if="canExcel" @click="exportExcel">导出excel</el-button>
        </template>
        <div class="model-panel">
          <div class="model-panel-title">
            <p>{{ $t('basic_information') }}</p>
          </div>
          <div class="model-panel-body">
            <basic-information :info="infoList"> </basic-information>
          </div>
        </div>

        <div class="model-panel">
          <div class="model-panel-title">
            <Tooltip :text="$t('detailInfo')"></Tooltip>
          </div>
          <div class="model-panel-body">
            <div class="flex_full_item">
              <!-- 表格 -->
              <page-table
                :refresh="tableInfo.refresh"
                :init-curpage="tableInfo.initCurpage"
                v-model:data="tableInfo.data"
                :pager="false"
                :check-box="false"
                :tab-index="true"
                :query="filterInfo.query"
                :field-list="tableInfo.fieldList"
                :isDynamic="true"
              >
                <template v-slot:col-price="scope">
                  <span v-if="scope.row.price || scope.row.price === 0">
                    {{ (scope.row.price || 0) + '元/' + scope.row.networkUnit }}
                  </span>
                  <span v-else>-</span>
                </template>
                <template v-slot:col-qty="scope">
                  {{ (scope.row.qty || 0) + scope.row.networkUnit }}
                </template>
              </page-table>
            </div>
          </div>
        </div>
      </NewFullScreen>
    </div>
    <modify
      v-if="modifyInfo.visible"
      v-model:visible="modifyInfo.visible"
      :title="modifyInfo.title"
      :tableKey="modifyInfo.tableKey"
      :orderId="modifyInfo.orderId"
      :purchAccording="infoList.data.purchAccording"
      :is-copy="modifyInfo.isCopy"
      @returnModify="returnModify"
    />
  </div>
</template>

<script setup name="rejectedOrderDetailView">
import {
  provincePayOrderDetailCustomizeListAll,
  provincePayOrderById,
  provincePayOrderSubmit,
  excelDetailProvincePayOrder,
} from '@/api/province'
import { getCustomizeSetting } from '@/api/customize'
import modify from './modify'
import _ from 'lodash'

const emits = defineEmits()
const { proxy } = getCurrentInstance()
const props = defineProps(['visible', 'orderId'])
const tableKey = ref(
  'provincePayOrderDetails-com.slip.spd.entity.vo.provincePlatform.ProvincePayOrderDetailVO'
)
const infoList = ref({
  data: {},
  fieldList: [
    { label: '支付单编号', value: 'orderNo', type: 'text' },
    { label: '省采支付单编号', value: 'provinceOrderNo', type: 'text' },
    { label: '支付单类型', value: 'payTypeValue', type: 'text' },
    { label: '供应商', value: 'supplierName', type: 'text' },
    { label: '状态', value: 'statusValue', type: 'text' },
    { label: '创建人', value: 'createUserName', type: 'text' },
    { label: '创建时间', value: 'createTime', type: 'text' },
    { label: '支付单备注', value: 'remark', type: 'text' },
  ],
})
// 过滤相关配置
const filterInfo = ref({
  sortArr: [],
  copyQueryMap: {},
  query: {
    queryMap: {
      orderId: {
        queryType: 'EQUALS',
        queryCategory: 'AND',
        value: null,
      },
    },
    queryParams: {},
  },
  searchList: [],
  searchBtn: [],
  btnList: [],
})

// 表格相关
const tableInfo = ref({
  api: undefined,

  fieldList: [],
  refresh: 1,
  initCurpage: 1,
  data: [],
})

const modifyInfo = ref({
  visible: false,
  title: '',
  orderId: '',
  tableKey: '',
  isCopy: false,
})

const showDialog = computed({
  get() {
    return props.visible
  },
  set(newValue) {
    emits('update:visible', newValue) // 更新父组件shows
  },
})

const canSubmit = computed(() => {
  return (
    proxy.$permission('provincePayOrder:submit') &&
    infoList.value.data.status === 0
  )
})
const canExcel = computed(() => {
  return (
    proxy.$permission('provincePayOrder:exportExcel') &&
    infoList.value.data.status !== 0
  )
})
setData()
function setData() {
  let obj = {
    commonName: {
      label: '通用名称',
      value: 'commonName',
      componentType: 'input',
      show: false,
      hidden: true,
    },
    price: {
      label: '省采单价',
      value: 'price',
      type: 'slot',
      formatter: proxy.$fn.TripartiteMethod,
      hidden: true,
    },
    qty: {
      label: '待结算数量',
      value: 'qty',
      type: 'slot',
      componentType: 'input',
      hidden: true,
    },
  }
  tableInfo.value.fieldList = Object.values({
    ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
    ...obj,
  })
  getCustomize()
}
function exportExcel() {
  excelDetailProvincePayOrder(filterInfo.value.query).then((resp) => {
    if (resp) {
      proxy.$fn.parseExcel(resp)
    }
  })
}
function returnModify() {
  if (props.orderId) {
    getCustomize()
  }
}

function getOrderInfo(orderId) {
  provincePayOrderById(orderId).then((resp) => {
    if (resp && resp.success) {
      infoList.value['data'] = resp.data
    }
  })
}
function update() {
  modifyInfo.value.visible = true
  modifyInfo.value.title = '修改省采订单支付'
  // 显示信息
  modifyInfo.value.orderId = props.orderId
}
function submit() {
  const ids = []
  ids.push(infoList.value.data.id)
  provincePayOrderSubmit(ids).then((resp) => {
    if (resp && resp.success) {
      getOrderInfo(props.orderId)
    }
  })
}

function getCustomize() {
  let _tableInfo = tableInfo.value
  let param = {
    tableKey: tableKey.value,
  }
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      _tableInfo.fieldList = proxy.$fn.setFieldList(
        _tableInfo.fieldList,
        data.detail
      )
      filterInfo.value.query.customizeUserVO = data
      filterInfo.value.query.queryMap.orderId.value = props.orderId
      getOrderInfo(props.orderId)
      getList()
    }
  })
}
function returnBack() {
  emits('returnBack', false)
}
function getList() {
  provincePayOrderDetailCustomizeListAll(filterInfo.value.query).then(
    (resp) => {
      if (resp && resp.success) {
        tableInfo.value.data = resp.data
      }
    }
  )
}
</script>
<style scoped lang="scss">
:deep(.page-table) > .el-table {
  overflow-y: hidden;
}
.model-panel-title {
  display: flex;
  align-items: center;
  .model-tips {
    display: flex;
    align-items: center;
    margin-left: 10px;
    color: #909399;
    .model-tips-nearly,
    .model-tips-expire {
      display: block;
      margin-left: 5px;
      width: 17px;
      height: 17px;
    }
    .model-tips-nearly {
      background: #fceacb;
    }
    .model-tips-expire {
      background: #f5e4e4;
    }
  }
}
:deep(.el-table) {
  .nearly-row {
    background: #fceacb;
  }
  .expire-row {
    background: #f5e4e4;
  }
}
</style>
